package xFGE.net
{
	import flash.events.EventDispatcher;
	
	import it.gotoandplay.smartfoxserver.SFSEvent;
	import it.gotoandplay.smartfoxserver.SmartFoxClient;

	public class SFSConnection extends EventDispatcher
	{
		protected var sfc:SmartFoxClient;
		
		public function SFSConnection()
		{
			sfc = new SmartFoxClient(true);
								
		}
		
		protected function add_onUserCountChange_Listener(fun:Function):void
		{
			sfc.addEventListener(SFSEvent.onUserCountChange,fun);
		}
		protected function add_onJoinRoom_Listener(fun:Function):void
		{
			sfc.addEventListener(SFSEvent.onJoinRoom,fun);
		}
		protected function onRoomListUpdate(fun:Function):void
		{
			sfc.addEventListener(SFSEvent.onRoomListUpdate,fun);
		}
		protected function add_onUserEnterRoom_Listener(fun:Function):void
		{
			sfc.addEventListener(SFSEvent.onUserEnterRoom,fun);
		}
		public function add_onExtensionResponse_Listener(fun:Function):void
		{
			sfc.addEventListener(SFSEvent.onExtensionResponse,fun);
		}
		protected function add_onConnection_Listener(fun:Function):void
		{
			sfc.addEventListener(SFSEvent.onConnection,fun);
		}
		protected function add_onConnectionLost_Listener(fun:Function):void
		{
			sfc.addEventListener(SFSEvent.onConnectionLost,fun);
		}
		protected function add_onObjectReceived_Listener(fun:Function):void
		{
			sfc.addEventListener(SFSEvent.onObjectReceived,fun);
		}
		protected function add_onConfigLoadSuccess_Listener(fun:Function):void
		{
			sfc.addEventListener(SFSEvent.onConfigLoadSuccess,fun);
		}
		protected function add_onConfigLoadFailure_Listener(fun:Function):void
		{
			sfc.addEventListener(SFSEvent.onConfigLoadFailure,fun);
		}
		protected function add_onRoomListUpdate_Listener(fun:Function):void
		{
			sfc.addEventListener(SFSEvent.onRoomListUpdate,fun);
		}
		protected function add_onUserLeaveRoom_Listener(fun:Function):void
		{
			sfc.addEventListener(SFSEvent.onUserLeaveRoom,fun);
		}
		protected function add_onRoomLeft_Listener(fun:Function):void
		{
			sfc.addEventListener(SFSEvent.onRoomLeft,fun);
		}
//		private function onJoinRoom(event:SFSEvent):void
//		{
//			trace("onJoinRoom");
//		}
//		private function onUserEnterRoom(event:SFSEvent):void
//		{
//			trace("User " + event.params.user.getName() + " entered the room");
//		}
//		private function onRoomListUpdate(event:SFSEvent):void
//		{
//			trace("onRoomListUpdate");
//			// Dump the names of the available rooms in the current zone
//    		for (var r:String in event.params.roomList)
//    		{
//        		trace(event.params.roomList[r].getName());
//      		}
//      		sfc.joinRoom("ddz_10");
//		}
//
//		public function addonExtensionResponse(fun:Function):void
//		{
//			sfc.addEventListener(SFSEvent.onExtensionResponse,fun);
//		}
//
//		private function onLogin(event:SFSEvent):void
//		{
//			trace("Login????")
//		}
//		
//		private function onConfigLoadFailure(event:SFSEvent):void
//		{
//			trace("Failed loading config file: " + event.params.message);
//		}
//		private function onConfigLoadSuccess(event:SFSEvent):void
//		{
//			trace("Config file loaded, now connecting...");
//		}
//		public function onAdminMessage(event:SFSEvent):void
//		{
//			trace("User " + event.params.getName() + " entered the room");
//		}
//		private function onConnection(event:SFSEvent):void
//		{
//			trace("成功连接SFS");
//			sfc.addEventListener(SFSEvent.onLogin,onLogin);
//			sfc.login("DDZ-ZONE","test_1","");
//			sfc.getRoomList();
//		}
//		
//		private function onConnectionLost(event:SFSEvent):void
//		{
//			
//		}
		public function connect():void
		{
			sfc.loadConfig("config/SmartFoxConfig.xml", true);	
            trace("开始连接SFS");	
//			sfc.connect("125.91.240.13");
//			sfc.defaultZone = "DDZ-ZONE";
		}
//		public function onObjectReceived(event:SFSEvent):void
//		{
//			trace("onObjectReceived");
//		}
		
		public function disconnect():void
		{
			if(sfc.isConnected == true)
			{
				sfc.disconnect();
//				if(sfc.getActiveRoom().getId() == 2)
//				{
//					sfc.joinRoom(sfc.getRoom(3).getName());
//				}
//				else
//				{
//					sfc.joinRoom(sfc.getRoom(2).getName());
//				}
			}
		}
//		public function sendMyPosition(posx:Number,posy:Number):void
//		{
//			var params:Object = new Object();
//			params.cmd = "pos";
//			params.posx = posx;
//			params.posy = posy;
//			params.speed = 10;
//			params.angle = 45;
//			
//			// Invoke "fire" command on the extension called "gameExt", using JSON protocol
//			trace("sfc.getActiveRoom().getId(): "+sfc.getActiveRoom().getId());
//			sfc.sendXtMessage("ddz", "position", params,"xml",sfc.getActiveRoom().getId());
//		}
		
		//////////////////////////////////////////////////////////////////////////////////////////////////////////////
		
		
	}
}